package com.itheoma.test;

import com.itheoma.utils.JdbcUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;

public class ZhuanZhang {
    public static void main(String[] args)throws Exception {
        Connection connection = null;
        PreparedStatement ps1 = null;
        PreparedStatement ps2 = null;
        try {
            connection = JdbcUtils.getConnection();
            connection.setAutoCommit(false);
            ps1 = connection.prepareStatement("update account set money = money - ? where id = ?");
            ps1.setObject(1,10);
            ps1.setObject(2,1);
            ps1.executeUpdate();


            ps2 = connection.prepareStatement("update account set money = money + ? where id = ?");
            ps2.setObject(1,10);
            ps2.setObject(2,2);
            ps2.executeUpdate();


            connection.commit();
        }catch (Exception e){
            connection.rollback();
            e.printStackTrace();
        }finally {
            connection.close();
            ps1.close();
            ps2.close();
        }

    }
}
